package org.zjh.view.rank;


import org.zjh.model.Player;
import org.zjh.model.Poker;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

import static org.zjh.constant.ImageConstant.RANK_IMAGE;
import static org.zjh.utils.MySQLUtil.getTop10PlayersInfo;

/**
 * @description: 排行榜面板
 * @author 郑佳豪
 * @date 2024/10/5 3:39
 * @version 1.0
 */
public class RankPanel extends JPanel {
    private ArrayList<Player> top10Players;


    public RankPanel(){
        //调用初始化构造方法
        init();
    }

    /**
     * 初始化
     */
    private void init(){
        top10Players = getTop10PlayersInfo();
    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(RANK_IMAGE.getImage(),0,0,RANK_IMAGE.getIconWidth(),RANK_IMAGE.getIconHeight(),null);
        //画出前十名玩家
        drawPlayerInfo(g);
    }


    /**
     * 画出前十玩家信息
     * @param g 画笔
     */
    private void drawPlayerInfo(Graphics g){
        //排行榜字体
        Font rankNameFont = new Font("黑体", Font.PLAIN, 40);
        g.setFont(rankNameFont);
        g.setColor(Color.BLACK);
        g.drawString("积分排行榜",250,200);
        //解释 字体
        Font explainFont = new Font("黑体", Font.PLAIN, 30);
        g.setFont(explainFont);
        g.drawString("排名",150,250);
        g.drawString("昵称",300,250);
        g.drawString("积分",450,250);
        //玩家信息字体
        Font playerInfoFont = new Font("黑体", Font.PLAIN, 20);

        g.setFont(playerInfoFont);
        int y = 300; // 起始Y坐标
        int rank = 1; //排名
        for (Player player : top10Players) {
            String noRank = "NO." + rank;
            String nickname = player.getNickname();
            String score = String.valueOf(player.getScore());
            // 设置积分绘制位置，使其对齐
            int scoreX = 465; // 积分列的X坐标

            g.drawString(noRank, 150, y);
            g.drawString(nickname,275, y);
            g.drawString(score, scoreX, y); // 积分在固定位置绘制

            y += 50; // 每行间隔
            rank++;
        }
    }
}
